/*
*	(*^。^*) TFrameWork License (*^。^*)
*/

/*
 *  Title :  
 *          
 *  Description :  
 *          
 *          
 *  Date : 
 *
 *  Author : DengPing
 *
 *  Modify : 
 */

using UnityEngine;
using Object = UnityEngine.Object;

namespace TFrameWork.Module.ActionEditor
{
    public class GameObjectEventItemStyle : CommonEventItemStyle
    {
        public override Color eventColor => new Color(0.49f, 0.83f, 0.98f, 0.4f);

        public override int RealLenth
        {
            get
            {
                if (prefabEvent == null)
                {
                    return 10;
                }

                return prefabEvent.GetDefaultDuration();
            }
        }

        public override TrackEventBase eventData => prefabEvent;
        public PrefabBaseEvent prefabEvent;

        public void Init(GameObjectTrackStyle.CommonChildTrackBase parent, PrefabBaseEvent data)
        {
            prefabEvent = data;
            InitComponent(parent);
        }


        public override void ShowContent()
        {
            base.ShowContent();
            if (prefabEvent.prefab)
            {
                if (!string.IsNullOrEmpty(prefabEvent.trackName))
                {
                    Name.text = prefabEvent.trackName;
                }
                else
                {
                    Name.text = prefabEvent.prefab.name;
                }
            }
            else
            {
                Name.text = "未设置画资源";
            }
        }


        #region 预览

        private GameObject pratcleObj;


        public override void HitEventPreView(int frameIndex)
        {
            //动画数据角色根节点
            var rootTransform = ActionEditor.Instance.PreviewCharacterObj.transform;
            //资源挂载的相对节点
            Transform relativeTran = rootTransform;
            if (prefabEvent.recordTransform != null)
            {
                relativeTran = prefabEvent.recordTransform;
            }
            else
            {
                //若根节点丢失，通过保存的名字查找根节点
                var findTran = ActionEditorUtility.FindChildTransformByNameIndex(
                    ActionEditor.Instance.PreviewCharacterObj.transform, prefabEvent.rocordTransformName);
                if (findTran != null)
                {
                    relativeTran = findTran;
                }
            }

            Vector3 pos;
            Quaternion rotate;
            Vector3 scale;
            
            if (ActionEditor.Instance.currentSelected != this && prefabEvent.recordEventDatas.TryGetValue(frameIndex, out var recordEventData))
            {
                //当有录制内容，且当前没有选中自己，播放录制内容
                var prefabMat = prefabEvent.localMatrix4X4;
                var mat = rootTransform.localToWorldMatrix * recordEventData.localToRelativeRootWrold * prefabMat;
                pos = mat.GetPosition();
                rotate = mat.GetRotation();
                scale = mat.GetScale();
            }
            else
            {
                //当选中或没有录制内容，通过相对节点计算
                
                var prefabMat = prefabEvent.localMatrix4X4;
                var prefablocalToWorldMat = relativeTran.transform.localToWorldMatrix * prefabMat;
                pos = prefablocalToWorldMat.GetPosition();
                rotate = prefablocalToWorldMat.GetRotation();
                scale = prefablocalToWorldMat.GetScale();
            }

            if (frameIndex == prefabEvent.startIndex && pratcleObj != null)
            {
                GameObject.DestroyImmediate(pratcleObj);
                pratcleObj = null;
            }

            if (pratcleObj == null)
            {
                pratcleObj = Object.Instantiate(prefabEvent.prefab, pos, rotate);
                pratcleObj.transform.localScale = scale;
            }

            //转换获取到特效位置和旋转
            pratcleObj.transform.position = pos;
            pratcleObj.transform.rotation = rotate;
            pratcleObj.transform.localScale = scale;

            //粒子模拟
            var systems = pratcleObj.GetComponentsInChildren<ParticleSystem>();
            for (int i = 0; i < systems.Length; i++)
            {
                int simulateFrame = frameIndex - prefabEvent.startIndex;
                systems[i].Simulate((float)simulateFrame / ActionEditor.Instance.ActionData.FrameRote);
            }

            if (frameIndex == prefabEvent.startIndex + prefabEvent.durationIndex && pratcleObj != null)
            {
                GameObject.DestroyImmediate(pratcleObj);
                pratcleObj = null;
            }
        }

        public override void TickPreView(int frameIndex)
        {
            if (!IsHit(frameIndex) && pratcleObj != null)
            {
                GameObject.DestroyImmediate(pratcleObj);
                pratcleObj = null;
            }
        }

        #endregion


    }
}
